package leecode;

import lombok.val;

import java.util.HashMap;
import java.util.Map;

/**
 * 383. 赎金信
 * easy
 */
public class _383 {
    public static void main(String[] args) {
        String ransomNote = "bg";
        String magazine = "efjbdfbdgfjhhaiigfhbaejahgfbbgbjagbddfgdiaigdadhcfcj";
        boolean res = canConstruct(ransomNote, magazine);
        System.out.println(res);
    }

    //todo 可用字符数组优化
    public static boolean canConstruct(String ransomNote, String magazine) {
        if (ransomNote.length() > magazine.length()) {
            return false;
        }
        Map<Character, Integer> m1 = new HashMap<>();
        for (int i = 0; i < ransomNote.length(); i++) {
            int count = m1.getOrDefault(ransomNote.charAt(i), 0);
            m1.put(ransomNote.charAt(i), ++count);
        }
        for (int i = 0; i < magazine.length(); i++) {
            Integer count = m1.get(magazine.charAt(i));
            if (null != count && count > 0) {
                m1.put(magazine.charAt(i), --count);
            }
        }
        for (Character key : m1.keySet()) {
            if (m1.get(key) > 0) {
                return false;
            }
        }
        return true;
    }
}